package com.bootdo.program.wallpaper.controller;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bootdo.common.controller.BaseController;
import com.bootdo.common.oss.AttachFileService;
import com.bootdo.common.oss.OssProcess;
import com.bootdo.common.oss.VideoFirstFrame;
import com.bootdo.program.film.domain.FilmCategoryDO;
import com.bootdo.program.film.service.FilmCategoryService;
import com.bootdo.program.wallpaper.domain.BzClassificationDO;
import com.bootdo.program.wallpaper.service.BzClassificationService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.program.wallpaper.domain.BzAllWallpaperDO;
import com.bootdo.program.wallpaper.service.BzAllWallpaperService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import org.springframework.web.multipart.MultipartFile;

/**
 * 小程序壁纸表(抖音、快手、微信)
 *
 * @author guoyunfei
 * @email 2549789059@qq.com
 * @date 2021-10-30 17:13:14
 */

@Controller
@RequestMapping("/wallpaper/bzAllWallpaper")
public class BzAllWallpaperController extends BaseController {

	private final String DIAGONAL = "/";

	@Autowired
	private BzAllWallpaperService bzAllWallpaperService;

	@Autowired
	private BzClassificationService bzClassificationService;

	@Autowired
	private AttachFileService attachFileService;

	@Autowired
	private FilmCategoryService filmCategoryService;


	@ResponseBody
	@GetMapping("/getCount")
	public R list(){
		Map<String, Object> params = new HashMap<>();
		params.put("status",1);
		//params.put("appid",queryAppid());

		int count1 = bzAllWallpaperService.count(params);
		params.put("status",2);
		int count2 = bzAllWallpaperService.count(params);
		params.put("status",3);
		int count3 = bzAllWallpaperService.count(params);

		return  R.ok().put("count1","待审核("+count1+")").put("count2","审核通过("+count2+")").put("count3","审核不通过("+count3+")");
	}


	@GetMapping()
	@RequiresPermissions("wallpaper:bzAllWallpaper:bzAllWallpaper")
	String BzAllWallpaper(Model model){

//		List<BzClassificationDO> cList = bzClassificationService.list(null);
//		model.addAttribute("cList",cList);

		List<FilmCategoryDO> list = filmCategoryService.list(null);
		model.addAttribute("cList",list);
		return "wallpaper/bzAllWallpaper/bzAllWallpaper";
	}


	@GetMapping("files")
	@RequiresPermissions("wallpaper:bzAllWallpaper:files")
	String files(Model model){
		Map<String,Object> map = new HashMap<>(4);
		map.put("enable",1);
		//map.put("appid",queryAppid());
		List<BzClassificationDO> cList = bzClassificationService.list(map);
		model.addAttribute("cList",cList);
		return "wallpaper/bzAllWallpaper/files";
	}

	@ResponseBody
	@GetMapping("/clazzList")
	public Object clazzList(){
		Map<String,Object> map = new HashMap<>(4);
		map.put("enable",1);
		//map.put("appid",queryAppid());
		List<BzClassificationDO> cList = bzClassificationService.list(map);
		return cList;
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wallpaper:bzAllWallpaper:bzAllWallpaper")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<BzAllWallpaperDO> list = bzAllWallpaperService.list(query);

		int total = bzAllWallpaperService.count(query);
		PageUtils pageUtils = new PageUtils(list, total);
		return pageUtils;
	}


	@ResponseBody
	@GetMapping("/list2")
	@RequiresPermissions("wallpaper:bzAllWallpaper:files")
	public PageUtils list2(@RequestParam Map<String, Object> params){
		//params.put("appid",queryAppid());
		//查询列表数据
		Query query = new Query(params);
		query.put("isSys",1);
		List<BzAllWallpaperDO> bzWallpaperList = bzAllWallpaperService.list(query);
		int total = bzAllWallpaperService.count(query);
		PageUtils pageUtils = new PageUtils(bzWallpaperList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	@RequiresPermissions("wallpaper:bzAllWallpaper:add")
	String add(){
	    return "wallpaper/bzAllWallpaper/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wallpaper:bzAllWallpaper:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		BzAllWallpaperDO bzAllWallpaper = bzAllWallpaperService.get(id);
		model.addAttribute("bzAllWallpaper", bzAllWallpaper);
	    return "wallpaper/bzAllWallpaper/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wallpaper:bzAllWallpaper:add")
	public R save( BzAllWallpaperDO bzAllWallpaper){
		if(bzAllWallpaperService.save(bzAllWallpaper)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wallpaper:bzAllWallpaper:edit")
	public R update( BzAllWallpaperDO bzAllWallpaper){
		bzAllWallpaperService.update(bzAllWallpaper);
		return R.ok();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/approved")
	@RequiresPermissions("wallpaper:bzAllWallpaper:edit")
	public R approved( BzAllWallpaperDO bzAllWallpaper){

		BzAllWallpaperDO bzAllWallpaperDO = bzAllWallpaperService.get(bzAllWallpaper.getId());
		bzAllWallpaperDO.setStatus(bzAllWallpaper.getStatus());
		bzAllWallpaperService.update(bzAllWallpaperDO);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wallpaper:bzAllWallpaper:remove")
	public R remove( Integer id) throws IOException {
		BzAllWallpaperDO wallpaperDO = bzAllWallpaperService.get(id);
		String thumbnail = wallpaperDO.getImage();
		if(bzAllWallpaperService.remove(id)>0){
			String fileName = thumbnail.substring(thumbnail.lastIndexOf("/") + 1);
			attachFileService.deleteFile(fileName);
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wallpaper:bzAllWallpaper:batchRemove")
	public R remove(@RequestParam("ids[]") Integer[] ids){

		try {
			for (int i = 0; i < ids.length; i++) {
				BzAllWallpaperDO wallpaperDO = bzAllWallpaperService.get(ids[i]);
				String thumbnail = wallpaperDO.getImage();
				if(bzAllWallpaperService.remove(ids[i])>0){
					String fileName = thumbnail.substring(thumbnail.lastIndexOf("/") + 1);
					attachFileService.deleteFile(fileName);
				}
			}
			//bzAllWallpaperService.batchRemove(ids);
			//bzAllWallpaperService.batchRemoveUpdate(ids);
		}catch (Exception e){

		}
		return R.ok();
	}

    /**
     * 批量审批
     */
    @PostMapping( "/batchEdit")
    @ResponseBody
    @RequiresPermissions("wallpaper:bzAllWallpaper:batchEdit")
    public R batchEdit(@RequestParam("ids[]") Integer[] ids,@RequestParam("status") Integer status){

        if(status == 2){
            bzAllWallpaperService.batchEdit(ids);
        }else{
            bzAllWallpaperService.batchEdit3(ids);
        }
        return R.ok();
    }


	@PostMapping("/upload/oss")
	@ResponseBody
	public R upload(@RequestParam("file") MultipartFile file, Integer classType, String className) throws Exception {
		String message = getMessage();
		if(null != message){
			return R.error(message);
		}

		if(null == classType){
			return R.error("请选择左侧栏目在进行上传");
		}

		String fileName = attachFileService.getDomain() + DIAGONAL + attachFileService.uploadFile(file);

		BzAllWallpaperDO bzWallpaper = new BzAllWallpaperDO();
		//bzWallpaper.setAppid(getAppid());
		bzWallpaper.setImage(fileName);

		//添加压缩参数
		String compress = OssProcess.getCompress(attachFileService.getOssType());
		bzWallpaper.setThumbnail(fileName + compress);

		String compressNoAd = OssProcess.getCompressNoAd(attachFileService.getOssType());
		bzWallpaper.setLowImageQuality(fileName + compressNoAd);
		bzWallpaper.setClassId(classType);
		bzWallpaper.setTime(new Date());
		bzWallpaper.setCreateTime(new Date());
		bzWallpaper.setTags(className);
		bzWallpaper.setImgSize(file.getSize());
		bzWallpaper.setIsSys(1);
		bzWallpaper.setStatus(2);
		bzWallpaper.setIsDelete(1);
		bzAllWallpaperService.save(bzWallpaper);

		return R.ok().put("fileName",fileName);
	}

}
